<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Http\Repositories\Services\IIm;
use App\Http\Repositories\Services\IUser;
use App\Http\Requests\Api\MobileBindingInputDTO;
use App\Http\Requests\Api\MobileReplaceInputDTO;
use App\Http\Requests\Api\UserAdminDTO;
use App\Http\Requests\Api\UserJobDTO;
use App\Http\Requests\Api\UserVerifyDTO;
use App\Util\Helper;
use App\Util\ResultsVo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;

class UserController extends Controller
{
    protected $userService;
    protected $messageService;
    protected $imService;

    public function __construct(
        IUser $userService,
        IIm $imService
    )
    {
        $this->userService = $userService;
        $this->imService = $imService;
    }

    public function binding(MobileBindingInputDTO $inputDTO){
        $this->userService->binding($inputDTO);
        return response()->json(new ResultsVo(['message'=>Lang::get('response.success')]));
    }

    // TODO 统计数据需要完善
    public function show(){
        $dao = $this->userService->info();
        #报价单合计
        $dao['offer'] = [
            'draft' => 0,           #草稿
            'ongoing' => 0,         #进行中
            'history' => 0,         #历史
            'cooperation' => 0      #合作的
        ];
        #最新动态
        $dao['new'] = [
            'in_buy' => 0,          #入驻维修企业
            'in_sell' => 0,         #入驻供应商
            'current_buy' => 0,     #当前询价的企业
            'current_sell' => 0     #当前报价的企业
        ];

        return response()->json(new ResultsVo(['data'=>$dao,'message'=>Lang::get('response.success')]));
    }

    public function info(){
        $dao = $this->userService->info();
        return response()->json(new ResultsVo(['data'=>$dao,'message'=>Lang::get('response.success')]));
    }

    public function fellow(Request $request){
        $search = [];
        $search['company_id'] = Auth::user()->company_id;
        $search['not_user_id'] = Auth::user()->id;
        $data = $this->userService->lists($search,$request->get('page',1),$request->get('size',10));

        $allow = ['id','nickname','mobile','avatar','job','job_name','is_admin','verify'];
        $dao = [];
        $dao['data'] = Helper::arrayField($data['results'],$allow);
        $dao['job'] = Config::get('constant.job');
        return response()->json(new ResultsVo(['data'=>$dao,'message'=>Lang::get('response.success')]));
    }

    public function verify(UserVerifyDTO $verifyDTO){
        $this->userService->verify($verifyDTO);
        return response()->json(new ResultsVo(['message'=>Lang::get('response.success')]));
    }

    public function admin(UserAdminDTO $adminDTO){
        $this->userService->admin($adminDTO);
        return response()->json(new ResultsVo(['message'=>Lang::get('response.success')]));
    }

    public function job(UserJobDTO $jobDTO){
        $this->userService->job($jobDTO);
        return response()->json(new ResultsVo(['message'=>Lang::get('response.success')]));
    }

    public function replace(MobileReplaceInputDTO $inputDTO){
        $this->userService->replace($inputDTO);
        return response()->json(new ResultsVo(['message'=>Lang::get('response.success')]));
    }

    public function test(){
        $this->userService->test();
        return response()->json(new ResultsVo(['message'=>Lang::get('response.success')]));
    }

    public function im(){
        $dao = $this->imService->imReg(Auth::user());
        return response()->json(new ResultsVo(['data'=>$dao,'message'=>Lang::get('response.success')]));
    }

}